package com.egg.ylt.adapter;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.egg.ylt.R;
import com.egg.ylt.Utils.StringSpanUtils;
import com.egg.ylt.Utils.StringUtil;
import com.egg.ylt.pojo.CouponEntity;
import com.yonyou.framework.library.adapter.rv.CommonAdapter;
import com.yonyou.framework.library.adapter.rv.ViewHolder;

/**
 * author:        yupeng
 * created on:    2019/3/1 14:45
 * description:   新优惠券列表
 */
public class ADA_NewCouponList extends CommonAdapter<CouponEntity.ListEntity> {

    public ADA_NewCouponList(Context context) {
        super(context);
    }

    @Override
    protected int itemLayoutId() {
        return R.layout.item_coupon_list;
    }

    @Override
    protected void convert(ViewHolder holder, final CouponEntity.ListEntity listBean, final int position) {

        float scale;
        if (listBean.getTicketAmount().length() > 5) {
            scale = 1.2f;
        } else {
            scale = 1.5f;
        }

        ((TextView) holder.getView(R.id.price_txt)).setText(StringSpanUtils.init()
                .addRelativeScale(scale, StringUtil.getBigDecimalPrice(listBean.getTicketAmount())).addStr(" ￥").build());
        holder.setText(R.id.coupon_name_txt, listBean.getTicketName());
        holder.setText(R.id.coupon_date_txt, listBean.getStartDate() + "至" + listBean.getEndDate());
        // 0-待使用，1-已使用 2-已过期 3-未领取
        switch (listBean.getStatus()) {
            case "3": // 未领取
                ((TextView)holder.getView(R.id.obtain_coupon_button)).setText("立即领取");
                holder.getView(R.id.obtain_coupon_button).setEnabled(true);
                holder.getView(R.id.obtain_coupon_button).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onButtonClickListener != null) {
                            onButtonClickListener.onClick(listBean, position);
                        }
                    }
                });
                break;
            case "0": // 待使用
                ((TextView)holder.getView(R.id.obtain_coupon_button)).setText("已领取");
                holder.getView(R.id.obtain_coupon_button).setEnabled(false);
                break;
        }
    }

    private OnButtonClickListener onButtonClickListener;

    public interface OnButtonClickListener {
        void onClick(CouponEntity.ListEntity model, int position);
    }

    public void setOnButtonClickListener(OnButtonClickListener onButtonClickListener) {
        this.onButtonClickListener = onButtonClickListener;
    }
}
